from model import *
from pybrain.tools.shortcuts import buildNetwork
from pybrain.structure import TanhLayer, SoftmaxLayer
from pybrain.datasets import SupervisedDataSet
from pybrain.supervised.trainers import BackpropTrainer

class ANN():
    inputs = 0
    hidden = 0
    outputs = 0
    net = None
    ds = None
    trainer = None
    times = 0
    errorrate = 1
    
    def __init__(self, input, hidden, output):
        self.inputs = input
        self.hidden = hidden
        self.outputs = output
        self.net = buildNetwork(input, hidden, output, hiddenclass=TanhLayer)
        self.ds = SupervisedDataSet(input, output)
        #self.trainer = BackpropTrainer(self.net, self.ds)

    def feed(self, inputset, outputset):
        self.ds.addSample(inputset,outputset)
        
    def train(self):
        if not self.trainer:
            self.trainer = BackpropTrainer(self.net, self.ds)
        self.times = self.trainer.totalepochs
        self.errorrate = self.trainer.train()
        return self.errorrate

    def trainNTimes(self,n):
        i = 0
        while i < n-1:
            self.train()
            i += 1
        self.errorrate = self.train()
        return self.errorrate
    
    def trainUntilStable(self, threshold=0.1, maxloop=5000):
        i = 0
        error = 1
        while i < maxloop and error > threshold:
            error = self.train()
            i += 1
        self.errorrate = error
        return error
    
    def test(self):
        pass
    
    def predict(self):
        pass
    